import csv
import gzip
import os
import pickle

domain  = 'devevent'
data_type = 'test'
isReverseCSV = True

# with gzip.GzipFile(f'./../../domains/{domain}/{data_type}.pkl', 'r') as f:
#     data = pickle.load(f)

# f = open(f'./../../domains/{domain}/{data_type}.pkl', 'rb') if not isReverseCSV else (
    # open(f'./output/{domain}/{data_type}.pkl', 'rb'))

f = open(f'/Users/haidilaolzx/Documents/Tsinghua_E/读硕士/毕业论文相关/代码/neural-datalog-through-time/anhp/data/train.pkl', 'rb')
data = pickle.load(f, encoding='latin1')
print(data)

list_length = len(data)
print("The number of ses(s) in the data is:", list_length)

# 首先，定义output文件夹的路径
output_dir = f'output/cat-pkl'

# 检查output文件夹是否存在，不存在则创建
if not os.path.exists(output_dir):
    os.makedirs(output_dir)

for i in range(list_length):
    print("The number of element(s) in the seq is:", len(data[i]))

    # 需要确保CSV文件的保存路径是output文件夹中
    output_csv_path = os.path.join(output_dir, f'{domain}-{data_type}_pkl_output-{i}.csv') if not isReverseCSV else (
        os.path.join(output_dir, f'{domain}-{data_type}_pkl_output-{i}_cp.csv'))
    # 现在，data应该是一个二维列表，其中包含字典
    # 接下来，将这些数据写入CSV文件
    with open(output_csv_path, 'w', newline='') as csv_file:
        # 创建一个csv.writer对象
        writer = csv.writer(csv_file)

        # 写入标题行，假设所有字典都有相同的键
        if data and data[i]:  # 检查data不为空且data[0]也不为空
            headers = data[i][0].keys()
            writer.writerow(headers)

        # 遍历data中的每个子列表
        for sublist in data[i]:
            writer.writerow(sublist.values())

    print(f"{list_length}-{i} Data has been written to {output_csv_path}")

